package io.sxl.modules.generator.service.impl;


import io.sxl.modules.generator.entity.HouseEntity;
import io.sxl.modules.generator.entityx.DiaoBoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.sxl.common.utils.PageUtils;
import io.sxl.common.utils.Query;

import io.sxl.modules.generator.dao.FamilyDao;
import io.sxl.modules.generator.entity.FamilyEntity;
import io.sxl.modules.generator.service.FamilyService;


@Service("familyService")
public class FamilyServiceImpl extends ServiceImpl<FamilyDao, FamilyEntity> implements FamilyService {

    @Autowired
    private FamilyDao dao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FamilyEntity> page = this.page(
                new Query<FamilyEntity>().getPage(params),
                new QueryWrapper<FamilyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryById(Map<String, Object> params) {
        ArrayList<FamilyEntity> familyEntities = new ArrayList<>();
        familyEntities.add(dao.queryById(Integer.parseInt(params.get("key").toString())));
        PageUtils pageUtils = new PageUtils(familyEntities, 1, 1, 1);

        return pageUtils;
    }

    @Override
    public String diaobo(DiaoBoEntity map) {
        List<Integer> ids = new ArrayList<>();
        Integer key = map.getKey();
        List<HouseEntity> houseEntities = map.getId();
        houseEntities.forEach((entity) -> {
            ids.add(entity.getId());
        });


        return null;
    }

    @Override
    public PageUtils queryByUser(Map<String, Object> params) {
        Integer page = Integer.parseInt((String)params.get("page"));
        Integer limit = Integer.parseInt((String) params.get("limit"));

        System.out.println("limit = " + limit);
        System.out.println("page="+page);
        params.replace("page", (page - 1) * limit);
        params.replace("limit",limit);


        List<HouseEntity> houseEntities = dao.queryByUser(params);
        Integer count = dao.queryByUserCount(params);
        PageUtils pageUtils = new PageUtils(houseEntities,count, limit, page);
        return pageUtils;
    }

    ;

}
